package cn.educate.service;

import java.util.*;
import java.util.stream.Collectors;

public class AICommandTaskManager {
    // 预定义任务类别及特征词
    private static final Map<String, List<String>> categoryRules = new HashMap<>() {{
        put("work", Arrays.asList("会议", "报告", "客户", "代码", "需求"));
        put("study", Arrays.asList("课程", "考试", "论文", "学习", "阅读"));
        put("life", Arrays.asList("旅行", "健身", "购物", "电影", "聚会"));
    }};

    // 用户行为记录（简单模拟）
    private final Map<String, Integer> userBehavior = new HashMap<>();

    // 任务管理系统
    private final List<Task> tasks = new ArrayList<>();
    public static class Task {
     public   final String description;
       public final String category;
       public final Date createdTime;
        Task(String desc, String cat) {
            description = desc;
            category = cat;
            createdTime = new Date();
        }
    }

    // 单例实例
    private static AICommandTaskManager instance = new AICommandTaskManager();

    private AICommandTaskManager() {}

    public static AICommandTaskManager getInstance() {
        return instance;
    }

    // AI推荐核心算法
    private String suggestCategory(String task) {
        task = task.toLowerCase();
        // 规则1：动词优先匹配
        if (task.contains("写")) return "work";
        if (task.contains("学")) return "study";
        if (task.contains("看")) return "life";

        // 规则2：关键词匹配
        for (Map.Entry<String, List<String>> entry : categoryRules.entrySet()) {
            if (entry.getValue().stream().anyMatch(task::contains)) {
                return entry.getKey();
            }
        }

        // 规则3：根据历史行为推荐
        return userBehavior.entrySet().stream()
                .max(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .orElse("others");
    }

    // 添加任务
    public void addTask(String taskDesc) {
        String suggestedCat = suggestCategory(taskDesc);
        System.out.printf("建议分类：%s，是否确认？(y/n) ", suggestedCat);
        // 模拟用户确认
        String confirm = "y"; // 简单模拟用户总是确认
        if (confirm.equals("y")) {
            tasks.add(new Task(taskDesc, suggestedCat));
            userBehavior.put(suggestedCat, userBehavior.getOrDefault(suggestedCat, 0) + 1);
            System.out.println("任务已添加");
        }
    }

    // 列出任务
    public List<Task> listTasks() {
        return tasks;
    }
}
